package com.qingmi.farmer.ui.webview;

import android.content.Context;
import android.content.Intent;
import android.net.http.SslError;
import android.text.TextUtils;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.qingmi.farmer.R;
import com.qingmi.farmer.databinding.ActivityWebviewBinding;
import com.wjjun.base.ui.base.BaseActivity;

/**
 * WebView 界面
 * Created by 67535 on 2018/3/21.
 */

public class WebViewActivity extends BaseActivity<WebViewViewModel, ActivityWebviewBinding> {

    private static final String INTENT_URL = "url";
    private static final String INTENT_TITLE = "title";

    public static void start(Context context, String url) {
        if (TextUtils.isEmpty(url)) {
            if (context instanceof BaseActivity) {
                ((BaseActivity) context).showToast(R.string.error_url_is_null);
            }
            return;
        }
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra(INTENT_URL, url);
        context.startActivity(intent);
    }

    public static void start(Context context, String url, String title) {
        if (TextUtils.isEmpty(url)) {
            if (context instanceof BaseActivity) {
                ((BaseActivity) context).showToast(R.string.error_url_is_null);
            }
            return;
        }
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra(INTENT_URL, url);
        intent.putExtra(INTENT_TITLE, title);
        context.startActivity(intent);
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_webview;
    }

    @Override
    protected WebViewViewModel initViewModel() {
        return new WebViewViewModel();
    }


    @Override
    protected void initContent() {
        super.initContent();
        viewDataBinding.webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                viewModel.progress.set(newProgress);
                if (newProgress >= 100 && TextUtils.isEmpty(viewModel.title.get())) {
                    viewModel.setTitle(viewDataBinding.webview.getTitle());
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (TextUtils.isEmpty(viewModel.title.get())) {
                    viewModel.setTitle(title);
                }
            }
        });

        viewDataBinding.webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (TextUtils.isEmpty(url) && url.startsWith("http")) {
                    viewModel.url.set(url);
                }
                return true;
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        viewModel.url.set(getIntent().getStringExtra(INTENT_URL));
        if (getIntent().hasExtra(INTENT_TITLE)) {
            viewModel.setTitle(getIntent().getStringExtra(INTENT_TITLE));
        }
    }

    @Override
    public void onBackPressed() {
        if (viewDataBinding.webview != null && viewDataBinding.webview.canGoBack()) {
            viewDataBinding.webview.goBack();
        } else {
            finish();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        viewDataBinding.webview.onDestory();
    }
}
